﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelnetRouterApplication.Model;
using TelnetRouterApplication.Control;
using TelnetRouterApplication.Utils;

namespace TelnetRouterApplication.View
{
    public partial class MonitorForm : UserControl
    {
        private MainForm _main;
        public MainForm Main
        {
            get { return _main; }
            set { _main = value; }
        }

        private SettingsModel _settings;
        public SettingsModel Settings
        {
            get { return _settings; }
            set { _settings = value; }
        }

        private List<Interface> _interfaceList;
        public List<Interface> InterfaceList
        {
            get { return _interfaceList; }
            set { _interfaceList = value; }
        }

        public MonitorForm(MainForm main)
        {
            Main = main;
            InitializeComponent();
            interfacesBox.DrawMode = DrawMode.OwnerDrawFixed;
            interfacesBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.interfaceListBoxDrawItem);

            CheckForIllegalCrossThreadCalls = false;

            this.Disposed += new System.EventHandler(this.DisposePanel);

            Thread thread = new Thread(new ThreadStart(this.ThreadAction));
            ThreadRun = true;
            thread.IsBackground = true;
            thread.Start();
        }

        private void DisposePanel(object sender, EventArgs e)
        {
            ThreadRun = false;
        }

        public MonitorForm() : this(null) { }

        private void GetInformation()
        {
            Settings = CommandHandler.GetInstance().GetSettings();
            InterfaceList = CommandHandler.GetInstance().GetInterfaces();

            this.routerNameBox.Text = Settings.Hostname;
            this.routerTimeBox.Text = Settings.MaskedClock;

            this.interfacesBox.Items.Clear();
            if (InterfaceList.Count > 0)
            {
                this.interfacesBox.BeginUpdate();
                foreach (Interface inter in InterfaceList)
                {
                    this.interfacesBox.Items.Add(inter);
                }
                this.interfacesBox.EndUpdate();
            }
        }

        private void settingsBtn_Click(object sender, EventArgs e)
        {
            Main.CallSettingsForm();
        }

        private void refreshBtn_Click(object sender, EventArgs e)
        {
            GetInformation();
        }

        private void testBtn_Click(object sender, EventArgs e)
        {
            Main.CallTestInterfacesForm();
        }

        private void exportBtn_Click(object sender, EventArgs e)
        {
            //Achei desnecessário checar se o settings é nulo

            string log = "";
            log += "Router Name@" + Settings.Hostname + "\r\n";
            log += "Router Time@" + Settings.MaskedClock + "\r\n";
            log += "\r\nInterfaces: \r\n\r\n";

            foreach (Interface inter in InterfaceList)
            {
                log += "Interface: " + inter.Name + " IP: " + inter.Ip + " Status: " + inter.IsTurnedON() + "\r\n";
            }

            string path = IOUtils.SaveFileDialogShow(Main, "Text Files|*.txt");
            IOUtils.CreateTxtFile(path, log);
            MessageBox.Show("Log exported!");
        }

        private void interfaceListBoxDrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            // Draw the background of the ListBox control for each item.
            // Create a new Brush and initialize to a Black colored brush
            // by default.

            e.DrawBackground();
            Brush myBrush = Brushes.Black;

            // Determine the color of the brush to draw each item based on 
            // the index of the item to draw.

            Interface inter = (Interface)interfacesBox.Items[e.Index];

            if (inter.IsTurnedON())
            {
                myBrush = Brushes.Green;
            }
            else
            {
                myBrush = Brushes.Red;
            }

            // Draw the current item text based on the current 
            // Font and the custom brush settings.
            //

            e.Graphics.DrawString("Interface: " + inter.Name + " IP: " + inter.Ip + " Status: " + inter.IsTurnedON(),
            e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);

            // If the ListBox has focus, draw a focus rectangle 
            // around the selected item.

            e.DrawFocusRectangle();
        }

        #region Thread Methods

        private bool _threadRun;
        public bool ThreadRun
        {
            get { return _threadRun; }
            set { _threadRun = value; }
        }

        private void ThreadAction()
        {
            while (ThreadRun)
            {
                GetInformation();

                if (MainForm.DEBUG)
                {
                    System.Diagnostics.Debug.WriteLine("Thread ... ");
                }

                Thread.Sleep(3000);
            }
        }

        //protected override Dis

        #endregion
    }
}
